<template>
  <div class="register-page" @mousemove="handleMouseMove">
    <!-- 背景图片 -->
    <div class="register-bg"></div>

    <!-- 品牌信息 -->
    <div class="brand-info">
      <img src="/transparentLogo.png" alt="系统Logo" class="brand-logo" />
      <div class="brand-slogan">
      </div>
    </div>

    <!-- 注册卡片 -->
    <div class="register-card">
      <div class="register-header">
        <h2 class="register-title">学生管理系统</h2>
        <!-- 眼部跟踪效果 -->
        <div class="eyes-container">
          <div class="eye left-eye" ref="leftEye">
            <div class="pupil" :style="leftPupilStyle"></div>
          </div>
          <div class="eye right-eye" ref="rightEye">
            <div class="pupil" :style="rightPupilStyle"></div>
          </div>
        </div>
      </div>

      <!-- 注册表单 -->
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" class="register-form"
        label-position="left" label-width="80px">
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名（3-20个字符）" prefix-icon="User"
            :disabled="loading" />
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码（至少6个字符）" prefix-icon="Lock"
            :disabled="loading" show-password @input="checkPasswordStrength" @focus="handlePasswordFocus"
            @blur="handlePasswordBlur" />

          <!-- 密码强度提示 -->
          <div v-if="registerForm.password" class="password-strength">
            <div class="strength-label">密码强度：</div>
            <div class="strength-bars">
              <div class="strength-bar" :class="passwordStrength >= 1 ? 'weak' : ''"></div>
              <div class="strength-bar" :class="passwordStrength >= 2 ? 'medium' : ''"></div>
              <div class="strength-bar" :class="passwordStrength >= 3 ? 'strong' : ''"></div>
            </div>
            <div class="strength-text">{{ getStrengthText }}</div>
          </div>
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" prefix-icon="Check"
            :disabled="loading" show-password @focus="handlePasswordFocus" @blur="handlePasswordBlur" />
        </el-form-item>

        <!-- 同意协议 -->
        <el-form-item prop="agree" class="agreement">
          <el-checkbox v-model="registerForm.agree" :disabled="loading">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>
            和
            <el-link type="primary">隐私政策</el-link>
          </el-checkbox>
        </el-form-item>

        <!-- 注册按钮 -->
        <el-form-item>
          <el-button type="primary" class="register-btn" @click="handleRegister" :loading="loading">
            注册
          </el-button>
          <div class="login-link">
            已有账号？<el-link type="primary" @click="toLogin">立即登录</el-link>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!-- 页脚信息 -->
    <div class="register-footer">
      <p>Copyright © 学生管理系统 2026-11-11</p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
// 导入注册接口
import { register } from '@/api/user';  // 假设你的接口文件路径是 src/api/user.js

// 组件挂载时添加进入动画
onMounted(() => {
  const registerCard = document.querySelector('.register-card');
  if (registerCard) {
    registerCard.style.opacity = '0';
    registerCard.style.transform = 'scale(0.9)';
    
    setTimeout(() => {
      registerCard.style.transition = 'all 0.5s cubic-bezier(0.4, 0, 0.2, 1)';
      registerCard.style.opacity = '1';
      registerCard.style.transform = 'scale(1)';
    }, 100);
  }
});

// 眼部跟踪相关
const leftEye = ref(null);
const rightEye = ref(null);
const mouseX = ref(0);
const mouseY = ref(0);
const isPasswordFocused = ref(false);

// 计算瞳孔位置
const calculatePupilPosition = (eyeElement, mouseX, mouseY) => {
  if (!eyeElement) return { x: 0, y: 0 };

  const eyeRect = eyeElement.getBoundingClientRect();
  const eyeCenterX = eyeRect.left + eyeRect.width / 2;
  const eyeCenterY = eyeRect.top + eyeRect.height / 2;

  // 计算鼠标相对于眼睛中心的角度
  const angle = Math.atan2(mouseY - eyeCenterY, mouseX - eyeCenterX);

  // 计算瞳孔移动的距离（限制在眼球范围内）
  const distance = Math.min(8, Math.hypot(mouseX - eyeCenterX, mouseY - eyeCenterY) / 10);

  // 计算瞳孔的最终位置
  const pupilX = Math.cos(angle) * distance;
  const pupilY = Math.sin(angle) * distance;

  return { x: pupilX, y: pupilY };
};

// 左眼瞳孔位置
const leftPupilStyle = computed(() => {
  if (isPasswordFocused.value) {
    // 输入密码时闭眼
    return {
      transform: 'translate(-50%, -50%) scaleY(0.1)',
      height: '2px',
      borderRadius: '50% / 10px'
    };
  }
  const position = calculatePupilPosition(leftEye.value, mouseX.value, mouseY.value);
  return {
    transform: `translate(calc(-50% + ${position.x}px), calc(-50% + ${position.y}px))`
  };
});

// 右眼瞳孔位置
const rightPupilStyle = computed(() => {
  if (isPasswordFocused.value) {
    // 输入密码时闭眼
    return {
      transform: 'translate(-50%, -50%) scaleY(0.1)',
      height: '2px',
      borderRadius: '50% / 10px'
    };
  }
  const position = calculatePupilPosition(rightEye.value, mouseX.value, mouseY.value);
  return {
    transform: `translate(calc(-50% + ${position.x}px), calc(-50% + ${position.y}px))`
  };
});

// 处理鼠标移动
const handleMouseMove = (event) => {
  mouseX.value = event.clientX;
  mouseY.value = event.clientY;
};

// 密码框焦点事件处理
const handlePasswordFocus = () => {
  isPasswordFocused.value = true;
};

const handlePasswordBlur = () => {
  isPasswordFocused.value = false;
};

// 注册表单数据
const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agree: false
});

// 密码强度
const passwordStrength = ref(0);

// 注册表单验证规则
const registerRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请同意用户协议和隐私政策'));
        } else {
          callback();
        }
      },
      trigger: 'change'
    }
  ]
});

// 表单引用和加载状态
const registerFormRef = ref(null);
const loading = ref(false);
const router = useRouter();

// 检查密码强度
const checkPasswordStrength = (value) => {
  let strength = 0;

  // 长度检查
  if (value.length >= 8) strength++;

  // 包含字母和数字
  if (/[A-Za-z]/.test(value) && /\d/.test(value)) strength++;

  // 包含特殊字符
  if (/[^A-Za-z0-9]/.test(value)) strength++;

  passwordStrength.value = strength;
};

// 获取密码强度文本
const getStrengthText = computed(() => {
  switch (passwordStrength.value) {
    case 0: return '未输入';
    case 1: return '弱';
    case 2: return '中';
    case 3: return '强';
    default: return '';
  }
});

// 前往登录页面
const toLogin = () => {
  // 添加收缩动画效果
  const registerCard = document.querySelector('.register-card');
  if (registerCard) {
    registerCard.style.transition = 'all 0.5s cubic-bezier(0.4, 0, 0.2, 1)';
    registerCard.style.transform = 'scale(0.95)';
    registerCard.style.opacity = '0.8';
    
    setTimeout(() => {
      router.push('/login');
    }, 300);
  } else {
    router.push('/login');
  }
};

// 注册处理函数 - 整合了真实接口调用
const handleRegister = async () => {
  // 表单验证
  try {
    await registerFormRef.value.validate();
  } catch (error) {
    // 表单验证失败
    return ElMessage.error('请完善注册信息');
  }

  // 显示加载状态
  loading.value = true;

  try {
    // 调用注册接口
    const response = await register({
      username: registerForm.username,
      password: registerForm.password
      // 注意：实际项目中通常不需要传递confirmPassword到后端
    });

    // 注册成功处理
    ElMessage.success('注册成功，请登录！');

    // 跳转到登录页
    setTimeout(() => {
      router.push('/login');
    }, 1000);

  } catch (error) {
    // 注册失败处理
    // 处理不同类型的错误信息
    const errorMsg = error.response?.data?.message ||
      error.message ||
      '注册失败，请重试';
    ElMessage.error(errorMsg);
  } finally {
    // 关闭加载状态
    loading.value = false;
  }
};
</script>

<style scoped>
/* 注册页面基础样式 */
.register-page {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: fixed;
  top: 0;
  left: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  margin: 0;
  overflow: hidden;
}

/* 背景图片 */
.register-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('/background_image.jpg');
  background-size: cover;
  background-position: center;
  filter: brightness(0.7);
  z-index: -1;
}

/* 品牌信息 */
.brand-info {
  position: fixed;
  top: -5%;
  left: 20px;
  color: #ffffff;
  text-align: left;
  z-index: 10;
}

.brand-logo {
  width: 200px;
  height: 200px;
  margin-bottom: 10px;
  border-radius: 8px;
}

.brand-slogan .slogan-text {
  font-size: 36px;
  font-weight: bold;
  margin: 0 0 10px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.brand-slogan .slogan-subtitle {
  font-size: 18px;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

/* 注册卡片样式 - 透明风格 */
.register-card {
  width: 100%;
  max-width: 400px;
  padding: 30px;
  border-radius: 12px;
  background-color: rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px);
  position: relative;
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid rgba(255, 254, 254, 0.5);
  transform-origin: center;
}

.register-card:hover {
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
  background-color: rgba(255, 255, 255, 0.4);
}

/* 注册卡片动画类 */
.register-card.shrink {
  animation: shrinkAnimation 0.5s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes shrinkAnimation {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(0.95);
    opacity: 0.9;
  }
  100% {
    transform: scale(0.95);
    opacity: 0.8;
  }
}

/* 注册头部样式 */
.register-header {
  text-align: center;
  margin-bottom: 35px;
}

.register-title {
  font-size: 28px;
  font-weight: bold;
  color: #ffffff;
  margin: 0;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  letter-spacing: 1px;
}

/* 表单样式 */
.register-form {
  width: 100%;
}

/* 表单项间距 */
.register-form .el-form-item {
  margin-bottom: 20px;
}

/* 表单标签样式 */
.register-form .el-form-item__label {
  color: #ffffff;
  font-weight: 500;
  font-size: 14px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  letter-spacing: 0.5px;
}

/* 输入框样式 */
.register-form :deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.9) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
  border-radius: 8px !important;
  transition: all 0.3s ease !important;
}

.register-form :deep(.el-input__wrapper:hover) {
  border-color: rgba(255, 255, 255, 0.5) !important;
  background-color: rgba(255, 255, 255, 0.95) !important;
}

.register-form :deep(.el-input__wrapper.is-focus) {
  border-color: #409EFF !important;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2), 0 2px 8px rgba(0, 0, 0, 0.15) !important;
  background-color: rgba(255, 255, 255, 1) !important;
}

.register-form :deep(.el-input__inner) {
  color: #303133 !important;
  font-size: 14px !important;
}

.register-form :deep(.el-input__inner::placeholder) {
  color: #909399 !important;
}

/* 密码输入框样式 */
.register-form :deep(.el-input--password .el-input__wrapper) {
  padding-right: 38px !important;
}

.register-form :deep(.el-input--password .el-input__suffix) {
  right: 12px !important;
}

/* 注册按钮样式 */
.register-btn {
  width: 100%;
  height: 46px;
  font-size: 17px;
  font-weight: 600;
  background-color: #409EFF;
  border: none;
  color: white;
  border-radius: 8px;
  transition: all 0.3s ease;
  letter-spacing: 1px;
}

.register-btn:hover {
  background-color: #66b1ff;
  transform: translateY(-1px);
  box-shadow: 0 3px 12px rgba(64, 158, 255, 0.4);
}

.register-btn.is-disabled {
  background-color: #c0c4cc;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 眼部跟踪效果样式 */
.eyes-container {
  position: relative;
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
  height: 50px;
  /* 固定容器高度 */
  pointer-events: none;
}

.eye {
  position: relative;
  width: 40px;
  /* 固定宽度 */
  height: 40px;
  /* 固定高度 */
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.9);
  border: 2px solid rgba(0, 0, 0, 0.3);
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.left-eye {
  /* 移除绝对定位，使用相对定位 */
}

.right-eye {
  /* 移除绝对定位，使用相对定位 */
}

.pupil {
  position: absolute;
  width: 16px;
  /* 固定瞳孔宽度 */
  height: 16px;
  /* 固定瞳孔高度 */
  background-color: #333;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: all 0.2s ease-out;
}

.pupil::after {
  content: '';
  position: absolute;
  width: 6px;
  /* 固定高光宽度 */
  height: 6px;
  /* 固定高光高度 */
  background-color: white;
  border-radius: 50%;
  top: 3px;
  left: 3px;
  opacity: 0.8;
}

/* 密码强度提示样式 */
.password-strength {
  display: flex;
  align-items: center;
  margin-top: 8px;
  font-size: 12px;
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

.strength-label {
  color: #ffffff;
  margin-right: 8px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

.strength-bars {
  display: flex;
  gap: 4px;
  margin-right: 8px;
}

.strength-bar {
  width: 30px;
  height: 4px;
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  transition: all 0.3s ease;
}

.strength-bar.weak {
  background-color: #f56c6c;
}

.strength-bar.medium {
  background-color: #e6a23c;
}

.strength-bar.strong {
  background-color: #67c23a;
}

.strength-text {
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

/* 复选框样式 */
.register-form :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #409EFF;
  border-color: #409EFF;
}

.register-form :deep(.el-checkbox__inner) {
  border-color: rgba(255, 255, 255, 0.5);
  background-color: rgba(255, 255, 255, 0.3);
}

.register-form :deep(.el-checkbox__label) {
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

/* 链接样式 */
.register-form :deep(.el-link) {
  color: #ffffff !important;
  text-decoration: underline;
}

.register-form :deep(.el-link:hover) {
  color: #e6f7ff !important;
}

/* 协议部分样式 */
.agreement {
  margin-top: 15px;
  margin-bottom: 15px;
}

/* 登录链接样式 */
.login-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  font-weight: 400;
}

.login-link :deep(.el-link) {
  color: #ffffff !important;
  margin-left: 5px;
  text-decoration: underline;
}

.login-link :deep(.el-link:hover) {
  color: #e6f7ff !important;
}

/* 页脚信息 */
.register-footer {
  position: fixed;
  bottom: 20px;
  text-align: center;
  width: 100%;
  left: 0;
  z-index: 10;
}

.register-footer p {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.9);
  margin: 0;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  font-weight: 300;
}

/* 密码强度检查样式 */
.password-strength {
  margin-top: 5px;
  font-size: 12px;
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

.strength-weak {
  color: #f56c6c;
}

.strength-medium {
  color: #e6a23c;
}

.strength-strong {
  color: #67c23a;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .brand-info {
    position: fixed;
    top: 15px;
    left: 15px;
    text-align: left;
  }

  .brand-logo {
    width: 70px;
    height: 70px;
  }

  .brand-slogan .slogan-text {
    font-size: 22px;
  }

  .brand-slogan .slogan-subtitle {
    font-size: 13px;
  }

  .register-card {
    width: 90%;
    max-width: 400px;
    padding: 25px;
    background-color: rgba(255, 255, 255, 0.4);
  }

  .register-title {
    font-size: 24px;
  }

  /* 眼睛大小保持固定，不随屏幕大小变化 */
  .eye {
    width: 40px;
    height: 40px;
  }

  .pupil {
    width: 16px;
    height: 16px;
  }

  .pupil::after {
    width: 6px;
    height: 6px;
  }

  .register-footer {
    position: static;
    margin-top: 30px;
  }

  .register-footer p {
    color: #ffffff;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  }
}

@media (max-width: 480px) {
  .register-page {
    padding: 15px;
  }

  .register-card {
    padding: 15px;
    max-width: 100%;
    border-radius: 6px;
  }

  .register-title {
    font-size: 22px;
  }

  .register-btn {
    height: 40px;
    font-size: 15px;
  }

  .register-form .el-form-item {
    margin-bottom: 15px;
  }

  /* 眼睛大小保持固定，不随屏幕大小变化 */
  .eye {
    width: 40px;
    height: 40px;
  }

  .pupil {
    width: 16px;
    height: 16px;
  }

  .pupil::after {
    width: 6px;
    height: 6px;
  }

  .brand-slogan .slogan-text {
    font-size: 28px;
  }

  .brand-slogan .slogan-subtitle {
    font-size: 16px;
  }
}
</style>
